/**
 * 
 */
package cl.duocap.refaxion.view.bean;

import java.io.Serializable;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import cl.duocap.refaxion.dto.TallerDTO;
import cl.duocap.refaxion.servicios.TallerService;

/**
 * @author Natali M
 * 
 * 
 */
@ManagedBean(name = "formTallerBean")
@SessionScoped
public class FormTallerBean implements Serializable {

	private static final long serialVersionUID = 1L;
	private TallerDTO tallerNuevo;
	private List<TallerDTO> listaTalleres;

	@PostConstruct
	public void iniciar() {
	   tallerNuevo = new TallerDTO();

		if (FacesContext.getCurrentInstance().isPostback()) {
			return;
		}
		TallerService taller = new TallerService();
		setListaTalleres(taller.obtenerTalleres());
	}

	public String registrarTallerNuevo() {
		
		SimpleDateFormat dateformatJava = new SimpleDateFormat("dd-MM-yyyy");
		tallerNuevo.setFechaIngreso(dateformatJava.format(tallerNuevo.getFechaIngresoTrans()));

		TallerService ingresarTaller = new TallerService();
		if (ingresarTaller.registrarTallerNuevo(tallerNuevo)) {
			return "mainTaller.xhtml?faces-redirect=true";
		} else {
			return "formTaller.xhtml?faces-redirect=true";
		}
	}

	public String listaTaller() {

		try {

			TallerService taller = new TallerService();
			setListaTalleres(taller.obtenerTalleres());
			return "formTaller.xhtml?faces-redirect=true";

		} catch (Exception e) {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR, e
							.getMessage(), e.getMessage()));
			System.out.println("errorLista Taller");
			return null;
		}

	}

	public FormTallerBean() {

	}

	public TallerDTO getTallerNuevo() {
		return tallerNuevo;
	}

	public void setTallerNuevo(TallerDTO tallerNuevo) {
		this.tallerNuevo = tallerNuevo;
	}

	public List<TallerDTO> getListaTalleres() {
		return listaTalleres;
	}

	public void setListaTalleres(List<TallerDTO> listaTalleres) {
		this.listaTalleres = listaTalleres;
	}

}
